<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
class AdminProjectController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/
	/**
	 * The layout that should be used for responses.
	 */
	protected $layout = 'layouts.admin';
	
	/**
	 * 显示导航project
	 */
	public function showProjects(){
		
		$content = View::make('admin.project.projects');
		$this->layout->content = $content;
		$projects = Project::tree();
		$content->with("projects",$projects);
	}
	
	public function showProjectAdd(){
		
		$content = View::make('admin.project.project-add');
		$this->layout->content = $content;
		$projects = Project::root()->get();
		
		$id = Input::get("id",0);
		if (empty($id)){
			$project = new Project();
		} else {
			$project = Project::find($id);
		}
		$content->with("project",$project);
		
		$content->with("projects",$projects)->with('colors',Config::get('mao.category_colors'));
	}
	
	public function postProject(){
		$p = Input::all();
		$id = Input::get('id',0);
		if (empty($id)){
			if (empty($p['order'])){
				$p['order'] = 0;
			}
			$project = Project::create($p);
		} else {
			//如果存在则更新
			$project = Project::find($id);
			$project->update($p);
		}
		return Redirect::route('project');
	}
	
	
	public function deleteProject(){
		$data = Input::all();
	
		$id = Input::get("id",0);
		
		$project = Project::find($id);
		$count = $project->children()->count();
		//没有子类别
		if ($count == 0){
			$project->delete();
			return Response::json(array('success'=>true));
		} else {
			//有子类别，不能删除
			return Response::json(array('success'=>false,'error'=>'存在sub-project不能删除!'));
		}
		
	}

}
